/**
 * 数据统计-贷款
 */
import './index.less'
import G2 from 'g2'
import $_fn from '~plugins'
export default ['$scope','$rootScope','$location','$ui','http',function($scope,$rootScope,$location,$ui,http) {
	$scope.tabMenu = $_fn.getCurrMenu($location.path())
  console.log($location.path())
	$scope.showloanCharts = function(){
		var data=$scope.pieChart.loanRatioPie
      var Stat = G2.Stat;
      var chart = new G2.Chart({
          id: 'loanCharts',
          forceFit: true,
          plotCfg: {
              margin: [90, 0, 60, 0]
          },
          height: 450,
      });
      chart.source(data);
      // 重要：绘制饼图时，必须声明 theta 坐标系
      chart.coord('theta', {
          radius: 0.7, // 设置饼图的大小
          inner: 0.6
      });
      chart.tooltip({
          title: null
      });
      chart.legend({
          position: 'top', // 设置图例的显示位置
          spacingX: 20,    // 图例项之间的水平间距
          dy:20,
          title: null,     // 不展示图例 title
          word: {
              fill: '#656363',
              fontSize: 14
          },
      });
      chart.intervalStack()
          .shape('burstPie')
          .position(Stat.summary.percent('money'))
          .color('name', ['#f3bc30', '#689be9'])
          .style({
              stroke: '#fff',
              lineWidth: 2
          }).label('name*..percent',function(name, percent){
          percent = (percent * 100).toFixed(2) + '%';
          return name + ':' + percent;
      });

      chart.render();
	}
	$scope.showcatchPlanCharts = function(){
			var data=$scope.pieChart.planPaymentPie
		 	// var data = [
	    //       { count: 0.55, name: '正常' },
	    //       { count: 0.45, name: '逾期' },
	    //   ];

      var Stat = G2.Stat;
      var chart = new G2.Chart({
          id: 'catchPlanCharts',
          forceFit: true,
          plotCfg: {
              margin: [90, 0, 60, 0]
          },
          height: 450,
      });
      chart.source(data);
      // 重要：绘制饼图时，必须声明 theta 坐标系
      chart.coord('theta', {
          radius: 0.7, // 设置饼图的大小
          inner: 0.6
      });
      chart.tooltip({
          title: null
      });
      chart.legend({
          position: 'top', // 设置图例的显示位置
          spacingX: 20,    // 图例项之间的水平间距
          dy:20,
          title: null,     // 不展示图例 title
          word: {
              fill: '#656363',
              fontSize: 14
          },
      });
      chart.intervalStack()
          .shape('burstPie')
          .position(Stat.summary.percent('money'))
          .color('name', ['#f3bc30', '#689be9', '#4cb85c'])
          .style({
              stroke: '#fff',
              lineWidth: 2
          }).label('name*..percent',function(name, percent){
          percent = (percent * 100).toFixed(2) + '%';
          return name + ':' + percent;
      });

      chart.render();
	}
	$scope.showcatchCharts = function(){
			var data=$scope.pieChart.paymentPie
		 	// var data = [
	    //       { money: 0.55, name: '正常' },
	    //       { money: 0.45, name: '逾期' },
	    //   ];
      //百分比计算}
      var Stat = G2.Stat;
      var chart = new G2.Chart({
          id: 'catchCharts',
          forceFit: true,
          plotCfg: {
              margin: [90, 0, 60, 0]
          },
          height: 450,
      });
      chart.source(data);
      // 重要：绘制饼图时，必须声明 theta 坐标系
      chart.coord('theta', {
          radius: 0.7, // 设置饼图的大小
          inner: 0.6
      });
      chart.tooltip({
          title: null
      });
      chart.legend({
          position: 'top', // 设置图例的显示位置
          spacingX: 20,    // 图例项之间的水平间距
          dy:20,
          title: null,     // 不展示图例 title
          word: {
              fill: '#656363',
              fontSize: 14
          },
      });
      chart.intervalStack()
          .shape('burstPie')
          .position(Stat.summary.percent('money'))
          .color('name', ['#f3bc30', '#689be9', '#4cb85c'])
          .style({
              stroke: '#fff',
              lineWidth: 2
          }).label('name*..percent',function(name, percent){
          percent = (percent * 100).toFixed(2) + '%';
          return name + ':' + percent;
      });

      chart.render();
	}
	$scope.showMoneyAllShow = function(){
			// var data = [
       //      {moneyType: 'Asia', data: '1750-12-12', money: 8},
       //      {moneyType: 'Asia', data: '1800', money: 9},
       //      {moneyType: 'Asia', data: '1850', money: 0},
       //      {moneyType: 'Africa', data: '1750', money: 1},
       //      {moneyType: 'Africa', data: '1800', money: 2},
       //      {moneyType: 'Africa', data: '1850', money: 3},
       //      {moneyType: 'Africa', data: '2050', money: 6},
       //      {moneyType: 'Europe', data: '1750', money: 6},
       //      {moneyType: 'Europe', data: '1800', money: 2},
       //      {moneyType: 'Europe', data: '1850', money: 3},
       //      {moneyType: 'Europe', data: '1950', money: 7},
       //      {moneyType: 'Europe', data: '1999', money: 9},
       //      {moneyType: 'Europe', data: '2050', money: 3},
       //    ];
   		var data =[]
      if($scope.lineChart && $scope.lineChart.loanAmountLine){
      	if($scope.lineChart.loanAmountLine.length==0){
      		data.push({moneyType:'放款金额'})
      	}
      	$scope.lineChart.loanAmountLine.forEach(function(val,key){
      		console.log('qqq',key,val)
      		val.moneyType = '放款金额'
      		data.push(val)
      	})
      }
      if($scope.lineChart && $scope.lineChart.badDebtLine ){
      	if($scope.lineChart.badDebtLine.length==0){
      		data.push({moneyType:'坏账金额'})
      	}
      	$scope.lineChart.badDebtLine.forEach(function(val,key){
      		console.log('qqq',key,val)
      		val.moneyType = '坏账金额'
      		data.push(val)
      	})
      }
      if($scope.lineChart && $scope.lineChart.overdueLine){
      	if($scope.lineChart.overdueLine.length==0){
      		data.push({moneyType:'逾期金额'})
      	}
      	$scope.lineChart.overdueLine.forEach(function(val,key){
      		console.log('qqq',key,val)
      		val.moneyType = '逾期金额'
      		data.push(val)
      	})
      }
      console.log('$scope.lineChart',$scope.lineChart)

      // var data=$scope.lineChart
      var Stat = G2.Stat;
      var chart = new G2.Chart({
        id: 'moneyAllShow',
        forceFit: true,
        height: 450
      });
      chart.source(data, {
        date: {
          title: null,
          type: 'time',
          mask: 'mm-dd',
        }
      });
      chart.areaStack().position('date*money').color('moneyType');
      chart.render();
	}

  $scope.showLoanTypeCharts = function(){
      var data=$scope.pieChart.getLoanWayPie
      // var data = [
      //       { count: 0.55, loan_way: '正常' },
      //       { count: 0.45, loan_way: '逾期' },
      //   ];

      var Stat = G2.Stat;
      var chart = new G2.Chart({
          id: 'loanTypeCharts',
          forceFit: true,
          plotCfg: {
              margin: [90, 0, 60, 0]
          },
          height: 450,
      });
      chart.source(data);
      // 重要：绘制饼图时，必须声明 theta 坐标系
      chart.coord('theta', {
          radius: 0.7, // 设置饼图的大小
          inner: 0.6
      });
      chart.tooltip({
          title: null
      });
      chart.legend({
          position: 'top', // 设置图例的显示位置
          spacingX: 20,    // 图例项之间的水平间距
          dy:20,
          title: null,     // 不展示图例 title
          word: {
              fill: '#656363',
              fontSize: 14
          },
      });
      chart.intervalStack()
          .shape('burstPie')
          .position(Stat.summary.percent('count'))
          .color('loan_way', ['#f3bc30', '#689be9', '#4cb85c'])
          .style({
              stroke: '#fff',
              lineWidth: 2
          }).label('loan_way*..percent',function(loan_way, percent){
          percent = (percent * 100).toFixed(2) + '%';
          return loan_way + ':' + percent;
      });

      chart.render();
  }
  $scope.showOrderStateCharts = function(){
      var data=$scope.pieChart.getOrderStatusPie
      // var data = [
      //       { count: 0.55, status: '正常' },
      //       { count: 0.45, status: '逾期' },
      //   ];

      var Stat = G2.Stat;
      var chart = new G2.Chart({
          id: 'orderStateCharts',
          forceFit: true,
          plotCfg: {
              margin: [90, 0, 60, 0]
          },
          height: 450,
      });
      chart.source(data);
      // 重要：绘制饼图时，必须声明 theta 坐标系
      chart.coord('theta', {
          radius: 0.7, // 设置饼图的大小
          inner: 0.6
      });
      chart.tooltip({
          title: null
      });
      chart.legend({
          position: 'top', // 设置图例的显示位置
          spacingX: 20,    // 图例项之间的水平间距
          dy:20,
          title: null,     // 不展示图例 title
          word: {
              fill: '#656363',
              fontSize: 14
          },
      });
      chart.intervalStack()
          .shape('burstPie')
          .position(Stat.summary.percent('count'))
          .color('status', ['#f3bc30', '#689be9', '#4cb85c'])
          .style({
              stroke: '#fff',
              lineWidth: 2
          }).label('status*..percent',function(status, percent){
          percent = (percent * 100).toFixed(2) + '%';
          return status + ':' + percent;
      });

      chart.render();
  }
  $scope.showLoanProduceCharts = function(){
      var data=$scope.pieChart.getLoanProductPie
      // var data = [
      //       { count: 0.55, status: '正常' },
      //       { count: 0.45, status: '逾期' },
      //   ];
      if(data && data.length>0){

        data.forEach(function(val, key){
          if(val.loan_type==1){
            val.status = '车贷'
          }
          else if(val.loan_type==2){
            val.status = '房贷'
          }
          else if(val.loan_type==3){
            val.status = '信贷'
          }
        })
      }
      var Stat = G2.Stat;
      var chart = new G2.Chart({
          id: 'loanProduceCharts',
          forceFit: true,
          plotCfg: {
              margin: [90, 0, 60, 0]
          },
          height: 450,
      });
      chart.source(data);
      // 重要：绘制饼图时，必须声明 theta 坐标系
      chart.coord('theta', {
          radius: 0.7, // 设置饼图的大小
          inner: 0.6
      });
      chart.tooltip({
          title: null
      });
      chart.legend({
          position: 'top', // 设置图例的显示位置
          spacingX: 20,    // 图例项之间的水平间距
          dy:20,
          title: null,     // 不展示图例 title
          word: {
              fill: '#656363',
              fontSize: 14
          },
      });
      chart.intervalStack()
          .shape('burstPie')
          .position(Stat.summary.percent('count'))
          .color('status', ['#f3bc30', '#689be9', '#4cb85c'])
          .style({
              stroke: '#fff',
              lineWidth: 2
          }).label('status*..percent',function(status, percent){
          percent = (percent * 100).toFixed(2) + '%';
          return status + ':' + percent;
      });

      chart.render();
  }
  $scope.showOrderTypeCharts = function(){
      var data=$scope.pieChart.getLoanTypePie
      // var data = [
      //       { count: 0.55, status: '正常' },
      //       { count: 0.45, status: '逾期' },
      //   ];

      var Stat = G2.Stat;
      var chart = new G2.Chart({
          id: 'orderTypeCharts',
          forceFit: true,
          plotCfg: {
              margin: [90, 0, 60, 0]
          },
          height: 450,
      });
      chart.source(data);
      // 重要：绘制饼图时，必须声明 theta 坐标系
      chart.coord('theta', {
          radius: 0.7, // 设置饼图的大小
          inner: 0.6
      });
      chart.tooltip({
          title: null
      });
      chart.legend({
          position: 'top', // 设置图例的显示位置
          spacingX: 20,    // 图例项之间的水平间距
          dy:20,
          title: null,     // 不展示图例 title
          word: {
              fill: '#656363',
              fontSize: 14
          },
      });
      chart.intervalStack()
          .shape('burstPie')
          .position(Stat.summary.percent('count'))
          .color('lt_name', ['#f3bc30', '#689be9', '#4cb85c'])
          .style({
              stroke: '#fff',
              lineWidth: 2
          }).label('lt_name*..percent',function(lt_name, percent){
          percent = (percent * 100).toFixed(2) + '%';
          return lt_name + ':' + percent;
      });

      chart.render();
  }
  $scope.showOrderFromCharts = function(){
      var data=$scope.pieChart.getOrderSourcePie
      // var data = [
      //       { count: 0.55, status: '正常' },
      //       { count: 0.45, status: '逾期' },
      //   ];

      var Stat = G2.Stat;
      var chart = new G2.Chart({
          id: 'orderFromCharts',
          forceFit: true,
          plotCfg: {
              margin: [90, 0, 60, 0]
          },
          height: 450,
      });
      chart.source(data);
      // 重要：绘制饼图时，必须声明 theta 坐标系
      chart.coord('theta', {
          radius: 0.7, // 设置饼图的大小
          inner: 0.6
      });
      chart.tooltip({
          title: null
      });
      chart.legend({
          position: 'top', // 设置图例的显示位置
          spacingX: 20,    // 图例项之间的水平间距
          dy:20,
          title: null,     // 不展示图例 title
          word: {
              fill: '#656363',
              fontSize: 14
          },
      });
      chart.intervalStack()
          .shape('burstPie')
          .position(Stat.summary.percent('count'))
          .color('os_name', ['#f3bc30', '#689be9', '#4cb85c'])
          .style({
              stroke: '#fff',
              lineWidth: 2
          }).label('os_name*..percent',function(os_name, percent){
          percent = (percent * 100).toFixed(2) + '%';
          return os_name + ':' + percent;
      });

      chart.render();
  }
  $scope.showPeriodsPercCharts = function(){
      var data=$scope.pieChart.getApplyTermPie
      // var data = [
      //       { count: 0.55, status: '正常' },
      //       { count: 0.45, status: '逾期' },
      //   ];

      var Stat = G2.Stat;
      var chart = new G2.Chart({
          id: 'periodsPercCharts',
          forceFit: true,
          plotCfg: {
              margin: [90, 0, 60, 0]
          },
          height: 450,
      });
      chart.source(data);
      // 重要：绘制饼图时，必须声明 theta 坐标系
      chart.coord('theta', {
          radius: 0.7, // 设置饼图的大小
          inner: 0.6
      });
      chart.tooltip({
          title: null
      });
      chart.legend({
          position: 'top', // 设置图例的显示位置
          spacingX: 20,    // 图例项之间的水平间距
          dy:20,
          title: null,     // 不展示图例 title
          word: {
              fill: '#656363',
              fontSize: 14
          },
      });
      chart.intervalStack()
          .shape('burstPie')
          .position(Stat.summary.percent('count'))
          .color('at_name', ['#f3bc30', '#689be9', '#4cb85c', '#e0594b'])
          .style({
              stroke: '#fff',
              lineWidth: 2
          }).label('at_name*..percent',function(at_name, percent){
          percent = (percent * 100).toFixed(2) + '%';
          return at_name + ':' + percent;
      });

      chart.render();
  }
	//数据请求
	$scope.init=function(fn){
		http.post('orderStatistics.do',{},function (data) {
    	console.log(data)
			if(data.errcode==1){
				// fn(data)
				$scope.$apply(function(){
					$scope.data = data.data
          $scope.dataCount = data.data.loanTotal
					$scope.chartsline = data.data.entryTrend
          $scope.chartsorder = $scope.data.orderStatus

					$scope.pieChart = data.data.pieChart
					$scope.lineChart = data.data.lineChart
				})
				$scope.showloanCharts()
				$scope.showcatchPlanCharts()
				$scope.showcatchCharts()
        $scope.showMoneyAllShow()

        $scope.showLoanTypeCharts()
        $scope.showOrderStateCharts()
        $scope.showLoanProduceCharts()

        $scope.showOrderTypeCharts()
        $scope.showOrderFromCharts()
				$scope.showPeriodsPercCharts()
			}else{
				$ui.toast({
					text:data.errmsg,
					type:'warning'
				})
			}
	  })

	}
	$scope.init()
	
	//
}]
